package com.woniu.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.woniu.been.entity.Courses;
import com.woniu.been.entity.vo.MyClassVO;

@Mapper
public interface CourseMapper {
    List<MyClassVO> findMyCourseById(Integer userId);
    int insertCourse(Courses course);
    int updateCourse(Courses course);
    int deleteCourse(Long courseId);
    /**
     * 根据分类ID获取课程列表
     * @param categoryId 分类ID
     * @return 课程列表
     */
    @Results({
            @Result(property = "courseId", column = "course_id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "description", column = "description"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "amendTime", column = "amend_time")
    })
    @Select("SELECT c.course_id, c.course_name, c.category_id, c.description, c.create_time, c.amend_time " +
            "FROM courses c " +
            "WHERE c.category_id = #{categoryId} " +
            "OR c.category_id IN (SELECT category_id FROM course_categories WHERE parent_category_id = #{categoryId})")
    List<Courses> findCoursesByCategoryId(Integer categoryId);

    /**
     * 根据分类名称获取课程列表
     * @param categoryName 分类名称
     * @return 课程列表
     */
    @Results({
            @Result(property = "courseId", column = "course_id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "description", column = "description"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "amendTime", column = "amend_time")
    })
    @Select("SELECT c.course_id, c.course_name, c.category_id, c.description, c.create_time, c.amend_time " +
            "FROM courses c " +
            "JOIN course_categories cc ON c.category_id = cc.category_id " +
            "WHERE cc.category_name = #{categoryName}")
    List<Courses> findCoursesByCategoryName(String categoryName);

    /**
     * 根据课程ID获取课程信息
     * @param courseId 课程ID
     * @return 课程信息
     */
    @Results({
            @Result(property = "courseId", column = "course_id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "description", column = "description"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "amendTime", column = "amend_time")
    })
    @Select("SELECT course_id, course_name, category_id, description, create_time, amend_time FROM courses WHERE course_id = #{courseId}")
    Courses findCourseById(Long courseId);

    /**
     * 根据章节ID获取课程列表（通过course_chapters_relations表关联）
     * @param chapterId 章节ID
     * @return 课程列表
     */
    @Results({
            @Result(property = "courseId", column = "course_id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "description", column = "description"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "amendTime", column = "amend_time")
    })
    @Select("SELECT c.course_id, c.course_name, c.category_id, c.description, c.create_time, c.amend_time " +
            "FROM courses c " +
            "JOIN course_chapters_relations ccr ON c.course_id = ccr.course_id " +
            "WHERE ccr.chapter_id = #{chapterId}")

    List<Courses> findCoursesByChapterId(Integer chapterId);
    @Select("SELECT course_id FROM user_course WHERE user_id = #{userId}")
    List<Integer> getMyCourseId(Integer userId);

} 
